package com.jxaummd.mushroom.main.common.presenter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.jxaummd.mushroom.R;
import com.jxaummd.mushroom.core.app.account.AccountManager;
import com.jxaummd.mushroom.core.app.application.University;
import com.jxaummd.mushroom.core.base.data.DataSource;
import com.jxaummd.mushroom.core.base.presenter.BasePresenter;
import com.jxaummd.mushroom.core.util.tool.storage.UniversityPreference;
import com.jxaummd.mushroom.core.util.tool.timer.BaseTimerTask;
import com.jxaummd.mushroom.core.util.tool.timer.ITimerListener;
import com.jxaummd.mushroom.main.common.contract.LaunchContract;
import com.jxaummd.mushroom.main.common.data.LaunchData;
import com.jxaummd.mushroom.main.common.fragment.LaunchScrollFragment;
import com.jxaummd.mushroom.main.common.fragment.LoginFragment;
import com.jxaummd.mushroom.main.common.model.LaunchModel;

import java.text.MessageFormat;
import java.util.Timer;

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;

public class LaunchPresenter extends BasePresenter<LaunchContract.View>
        implements  LaunchContract.Presenter,DataSource.Callback<LaunchModel>,ITimerListener {
    private Context mContext ;
    // 定时任务
    private BaseTimerTask mBaseTimerTask;
    //定时者
    private Timer mTimer;

    private int mCount = 3;

    private Handler mHandler = University.getHandler();

    public LaunchPresenter(LaunchContract.View view,Context context) {
        super(view);
        mContext=context;
    }

    @Override
    public void start() {
        getView().setNoStatusBar();
//        LaunchData.updateLanch(this);
        initTimer();
    }

    @Override
    public void onDataLoaded(LaunchModel launchModel) {
        String url = launchModel.getmImageUrl();
        final SimpleTarget<Drawable> simpleTarget = new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                getView().changeBackGround(resource);
            }
        };

        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.ic_launcher)
                ;

        Glide.with(mContext)
                .load(url)
                .transition(withCrossFade())
                .apply(options)
                .into(simpleTarget);
    }


    /**
     * 初始化定时器
     */
    private void initTimer() {
        mTimer = new Timer();
        mBaseTimerTask = new BaseTimerTask(this);
        mTimer.schedule(mBaseTimerTask, 0, 1000);
    }

    @Override
    public void onDataNotAvailable(int strRes) {
            getView().showError(strRes);
    }

    @Override
    public void onTimer() {
        if (getView() != null) {
        mHandler.post(() -> {
                getView().updateTimer(MessageFormat.format("跳过\n{0}", mCount));
                mCount--;
                if (mCount < 0) {
                    mTimer.cancel();
                    checkIsShowScroll();
                }
        });
        }
    }


    /**
     * 检查是否启动登录
     */
    private void checkIsShowScroll() {
        if (!UniversityPreference.getAppFlag(ScrollLunacherTag.HAS_FIRST_LUNCHER_APP.name())) {
            //启动LaunchScrollFragment
            getView().startFragment(new LaunchScrollFragment());
        } else {
            //检查登录
            if (!AccountManager.isSignIn()) {
                getView().startFragment(new LoginFragment());
            } else {
                getView().startActivity();
            }
        }
    }

    @Override
    public void destroy() {
        super.destroy();
        if(mTimer!=null){
            mTimer.cancel();
            mTimer=null;
        }
    }

    @Override
    public void skipTimer() {
        checkIsShowScroll();
    }

   public enum ScrollLunacherTag {
        /**
         * 判断是否是第一次进入APP
         */
        HAS_FIRST_LUNCHER_APP
    }

}
